package ctgu.yao.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ctgu.yao.cloud.dto.PageDTO;
import ctgu.yao.cloud.dto.ResultDTO;
import ctgu.yao.cloud.form.AdminForm;
import ctgu.yao.cloud.form.RuleForm;
import ctgu.yao.cloud.mapper.CompanyuserMapper;
import ctgu.yao.cloud.po.Admin;
import ctgu.yao.cloud.mapper.AdminMapper;
import ctgu.yao.cloud.po.Companyuser;
import ctgu.yao.cloud.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Y
 * @since 2022-08-16
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private CompanyuserMapper  companyuserMapper;
     //登录
    @Override
    public ResultDTO login(RuleForm ruleForm){
        //判断用户名是否存在
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",ruleForm.getUsername());
        Admin admin = this.adminMapper.selectOne(queryWrapper);
        ResultDTO resultDTO = new ResultDTO<>();
        if(admin == null){
            resultDTO.setCode(-1);
        }else{
            //判断密码是否正确
            if(!admin.getPassword().equals(ruleForm.getPassword())) {
                resultDTO.setCode(-2);
            }else {
                resultDTO.setCode(0);
                resultDTO.setData(admin);
            }
        }
        return  resultDTO;
    }

    @Override
    public PageDTO list(Integer page, Integer size,Integer mid) {
        Page<Companyuser> companyuserPage=new Page<>(page,size);
        QueryWrapper<Companyuser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mid",mid);
        Page<Companyuser> resultPage= this.companyuserMapper.selectPage(companyuserPage, queryWrapper);
       PageDTO pageDTO=new PageDTO();
       pageDTO.setTotal(resultPage.getTotal());
       pageDTO.setData(resultPage.getRecords());
       return  pageDTO;


    }

    @Override
    public PageDTO search(AdminForm adminForm) {
        Page<Companyuser> companyuserPage=new Page<>(adminForm.getPage(),adminForm.getSize());
        Page<Companyuser> resultPage;
        if(adminForm.getValue().equals("")){
            QueryWrapper<Companyuser> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("mid",adminForm.getMid());
            resultPage= this.companyuserMapper.selectPage(companyuserPage, queryWrapper);
        }
        else{
            QueryWrapper<Companyuser> queryWrapper=new QueryWrapper<>();
            queryWrapper.like(adminForm.getKey(),adminForm.getValue())
                        .eq("mid",adminForm.getMid());
            resultPage= this.companyuserMapper.selectPage(companyuserPage, queryWrapper);
        }
        PageDTO pageDTO=new PageDTO();
        pageDTO.setTotal(resultPage.getTotal());
        pageDTO.setData(resultPage.getRecords());
        return  pageDTO;
    }

    @Override
    public ResultDTO ComDelete(Integer cuid) {
        QueryWrapper<Companyuser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cuid",cuid);
        ResultDTO resultDTO = new ResultDTO<>();
        int delete = this.companyuserMapper.delete(queryWrapper);
        if(delete>0){
            resultDTO.setData(null);
            resultDTO.setCode(0);
        }
        else {
            resultDTO.setCode(-1);
        }
     return resultDTO;
    }



}
